package com.hdoit.ares.web.acti.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acti.model.Activity;
import com.hdoit.ares.logical.acti.service.IActivityService;
import com.hdoit.ares.logical.viewmodel.acti.ActivitySearch;
import com.hdoit.ares.selfcommon.enums.acti.ActiState;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年03月10日 19:38:03
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/acti/activity/")
public class ActivityController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/acti/activity/";
    }

    private IActivityService activityService;
    public IActivityService getActivityService() {
        return activityService;
    }
    @Resource
    public void setActivityService(IActivityService activityService) {
        this.activityService = activityService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="activityDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String toConfirmViewName = "toConfirm";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ActivitySearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(activityService.queryCount(search));
        List<Activity> activitys= activityService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("activitys", activitys);
        model.addAttribute("activitySearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String activityDetailed(Model model,String key){
        Activity activity=new Activity();
        if(!StringUtils.isEmpty(key)){
            activity=activityService.queryModel(key);
        }
        model.addAttribute("activity", activity);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})	
    public String save(Model model,Activity activity){
        if(StringUtils.isEmpty(activity.getId())){
        	activity.setId(CommonUtils.getKeyId());
        	activity.setCreateDate(DateUtils.getCurrent());
        	activity.setCreateUserId(getLoginModel().getLoginId());
        	activity.setCreateUserName(getLoginModel().getLoginName());
        	activity.setConfirmState(ConfirmState.unConfirm);
        	activity.setActiState(ActiState.down);
            activity.setModelStatus(ModelStatus.add);
        }else{
            activity.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(activityService.applyChange(activity)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("activity", activity);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(activityService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value =toConfirmViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    @ResponseBody 
    public String toConfirm(Model model,String id){
    	
    	System.err.println("jinru queren");
    	
    	Activity activity = activityService.queryModel(id);

    	activity.setActiState(ActiState.up);
    	activity.setConfirmState(ConfirmState.confirmed);
    	activity.setModelStatus(ModelStatus.update);
    	if(!activityService.applyChange(activity)){
    		return new JsonActionResult(ManageContainer.ERROR).toJson();
    	}
    	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
	
    }
}